<?php

namespace App\Http\Controllers\Api\Website;


use App\AreaLand;
use App\Http\Resources\AreaCollection;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\LandCollection;
use App\Http\Resources\LandResource;
use App\Repository\LandRepository;
use App\BrowseRecords;
use App\model\Land;
use App\User;
use App\UserSearch;
use Carbon\Carbon;
use App\model\ImSet;

class LandController extends Controller
{
    //
    public function index(Request $request,LandRepository $landRepository){
        $landList=$landRepository->getList($request);
        $user_id = auth('api')->id();
        if(isset($request['search_key'])&&$request['search_key']!=''&&isset($user_id)){
            $now = Carbon::now();
            UserSearch::updateOrCreate(['user_id'=>$user_id,'search_key'=>$request['search_key']],['updated_at'=>$now]);
        }
        return new LandCollection($landList);
    }
    public function detail(LandRepository $landRepository,$id){
        $land=$landRepository->detail($id);
        $user_id = auth('api')->id();
        $land['im_set']=ImSet::find(1);
        if(isset($user_id)){
            $lands = Land::find($id);
            $title = $lands->title;
            $logo = $lands->logo;
            $now = Carbon::now();
            BrowseRecords::updateOrCreate(['user_id'=>$user_id,'produce_id'=>$id,'type'=>1],
                ['user_name'=>User::find($user_id)->name,'title'=>$title,'logo'=>$logo,'updated_at'=>$now]);
        }
        return new LandResource($land);
    }
    public function area(){
      $area=  AreaLand::all();
      return new AreaCollection($area);
    }
}
